package cn.gy.productservice.controller;

import cn.gy.common.model.Product;
import cn.gy.productservice.SerciceException.ProductException;
import cn.gy.productservice.service.ProductService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品的服务控制层
 *
 * @author gy
 */
@AllArgsConstructor
@RestController
@Slf4j
public class ProductController {

    private final ProductService productService;

    /**
     * 根据商品id查询商品
     */
    @GetMapping("/findByProductId/{productId}")
    public Product findByProductId(@PathVariable Long productId) {
        Product product = null;
        product = productService.findByProductId(productId);
        log.info("-------Find " + productId);
        return product;
    }

    /**
     * 根据商品名称查询商品
     */
    @GetMapping("/findByProductName/{nm}")
    public Product findByProductName(@PathVariable String nm) {
        Product product = productService.findByProductName(nm);
        if (product != null) {
            log.info("-------Find " + nm);
        } else {
            log.info("----not found:" + nm);
        }
        return product;
    }

    /**
     * 查询所有商品
     */
    @GetMapping("/queryAllProduct")
    public List<Product> queryAllProduct() {
        List<Product> productList = null;
        try {
            productList = productService.queryAllProduct();
            log.info("---OK   queryAllProduct----");
        } catch (ProductException productException) {
            log.info("---Not found queryAllProduct ");
        }
        return productList;
    }
}
